﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using Entity;

namespace DAO
{
    public class QuanLyNCCDAO
    {
        public static DataTable LayDanhSachNCC()
        {
            SqlConnection connect = null;
            DataTable rs = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }
                String cmdText = "SELECT * FROM NHA_CUNG_CAP";
                rs = SqlHelper.ExecuteDataset(connect, CommandType.Text, cmdText).Tables[0];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (rs != null)
                    ((IDisposable)rs).Dispose();

                if (connect != null)
                    connect.Dispose();
            }
            return rs;
        }

        public static void SuaNCC(NhaCungCap ncc)
        {
            SqlConnection connect = null;
            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] param = new SqlParameter[5];
                param[0] = new SqlParameter("@mancc", ncc.mancc);
                param[1] = new SqlParameter("@tenncc", ncc.tenncc);
                param[2] = new SqlParameter("@diachi", ncc.diachi);
                param[3] = new SqlParameter("@sodienthoai", ncc.sodienthoai);
                param[4] = new SqlParameter("@email", ncc.email);
                String cmdText = "UPDATE NHA_CUNG_CAP SET TenNCC=@tenncc,DiaChi=@diachi,SoDienThoai=@sodienthoai,Email=@email " +
                                    "WHERE MaNCC = @mancc";
                SqlHelper.ExecuteNonQuery(connect, CommandType.Text, cmdText, param);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }

        public static void XoaNCC(Int32 mancc)
        {
            SqlConnection connect = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@mancc", mancc);

                String cmdText = "DELETE NHA_CUNG_CAP WHERE MaNCC = @mancc";

                SqlHelper.ExecuteNonQuery(connect, CommandType.Text, cmdText, par);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }

        public static void ThemNCC(NhaCungCap ncc)
        {
            SqlConnection connect = null;

            try
            {
                try
                {
                    connect = Connection.GetConnection();
                }
                catch
                {
                    throw new Exception("The connection with the database can´t be established");
                }

                SqlParameter[] par = new SqlParameter[4];
                par[0] = new SqlParameter("@tenncc", ncc.tenncc);
                par[1] = new SqlParameter("@diachi", ncc.diachi);
                par[2] = new SqlParameter("@dienthoai", ncc.sodienthoai);
                par[3] = new SqlParameter("@email", ncc.email);

                String cmdText = "INSERT INTO NHA_CUNG_CAP VALUES (@tenncc, @diachi, @dienthoai, @email)";

                SqlHelper.ExecuteNonQuery(connect, CommandType.Text, cmdText, par);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (connect != null)
                    connect.Dispose();
            }
        }

    }
}
